﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace AutoUpdate.util
{
    class XMLUtil
    {
        //public string releaseFile = "ReleaseList.xml";
        string rootNodeName = "AutoUpdater";
        public string serverURL = "";
        public string releaseDate = "";
        public string releaseVersion = "";
        public string applicationStart = "";
        public string shortcutIcon = "";

        public ArrayList desList = new ArrayList();
        public ArrayList releaseFileList = new ArrayList();

        public XMLUtil(String xmlFile)
        {
            readReleaseFile(xmlFile);
        }

        private void readReleaseFile(String xmlFile)
        {
            XmlDocument doc = new XmlDocument();

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            //xmlFilePath:xml文件路径  
            XmlReader reader = XmlReader.Create(xmlFile, settings);

            doc.Load(reader);

            serverURL = doc.SelectSingleNode("/"+ rootNodeName + "/ServerURL").InnerText;
            releaseDate = doc.SelectSingleNode("/"+ rootNodeName + "/ReleaseDate").InnerText;
            releaseVersion = doc.SelectSingleNode("/"+ rootNodeName + "/ReleaseVersion").InnerText;
            applicationStart = doc.SelectSingleNode("/"+ rootNodeName + "/ApplicationStart").InnerText;
            shortcutIcon = doc.SelectSingleNode("/"+ rootNodeName + "/ShortcutIcon").InnerText;

            //描述list
            XmlNode desNodes = doc.SelectSingleNode("/"+ rootNodeName + "/UpdateDes");
            XmlNodeList desChilddNodes=desNodes.ChildNodes;
            foreach (XmlNode node in desChilddNodes)
            {
                desList.Add(node.InnerText);
            }

            //文件list
            XmlNode releaseFileNodes = doc.SelectSingleNode("/" + rootNodeName + "/ReleaseFiles");
            XmlNodeList filesChilddNodes = releaseFileNodes.ChildNodes;
            XmlElement element = null;
            foreach (XmlNode node in filesChilddNodes)
            {
                element = (XmlElement)node;
                releaseFileList.Add(element.GetAttribute("name").ToString()+"@"+element.GetAttribute("size").ToString());
            }
            reader.Close();
        }
    }
}
